<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
<link rel="stylesheet" href="hd/index.css">
</head>
<body>
<div id="app">
    <div style="margin-top: 20px">
        <el-button size="small" type="primary" @click="add">新增</el-button>
        <el-button size="small" type="primary" @click="gen">生成代码</el-button>
        <el-button size="small" type="danger" @click="remove">批量删除</el-button>
    </div>
    <el-table
            ref="multipleTable"
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange">
        <el-table-column
                type="selection"
                width="55">
        </el-table-column>
<!--        <el-table-column-->
<!--                label="日期"-->
<!--                width="120">-->
<!--            <template slot-scope="scope">{{ scope.row.date }}</template>-->
<!--        </el-table-column>-->
        <el-table-column
                prop="id"
                label="id"
                width="120">
        </el-table-column>
<!--        <el-table-column-->
<!--                prop="mainPath"-->
<!--                label="主路径"-->
<!--                width="120">-->
<!--        </el-table-column>-->
        <el-table-column
                prop="bao"
                label="包名"
                width="120">
        </el-table-column>
        <el-table-column
                prop="moduleName"
                label="模块名"
                width="120">
        </el-table-column>
        <el-table-column
                prop="tablePrefix"
                label="表前缀"
                width="120">
        </el-table-column>
        <el-table-column
                prop="tableName"
                label="表名"
                width="120">
        </el-table-column>

        <el-table-column
                prop="datasourceId"
                label="数据源id"
                width="120">
        </el-table-column>

<!--        <el-table-column-->
<!--                prop="address"-->
<!--                label="地址"-->
<!--                show-overflow-tooltip>-->
<!--        </el-table-column>-->
        <el-table-column label="操作">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        @click="handleEdit(scope.row)">编辑</el-button>
                <el-button
                        size="mini"
                        type="success"
                        @click="yulan(scope.row)">在线预览</el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form :model="form">
<!--            <el-form-item label="主路径">-->
<!--                <el-input v-model="form.mainPath"></el-input>-->
<!--            </el-form-item>-->
            <el-form-item label="包名">
                <el-input v-model="form.bao"></el-input>
            </el-form-item>
            <el-form-item label="模块名">
                <el-input v-model="form.moduleName"></el-input>
            </el-form-item>
            <el-form-item label="表前缀">
                <el-input v-model="form.tablePrefix"></el-input>
            </el-form-item>
            <el-form-item label="表名">
                <el-input v-model="form.tableName"></el-input>
            </el-form-item>
            <el-form-item label="数据源">
                <el-select v-model="form.datasourceId" placeholder="请选择">
                    <el-option
                            v-for="item in options"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="submit">确 定</el-button>
        </div>
    </el-dialog>


    <el-dialog
            title="提示"
            :visible.sync="yulanDialogFormVisible"
            width="100%"
            >
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">

            <el-tab-pane
                    v-for="(value, key) in codeView"
                    :label="key"
                    :name="key"
                    :key="key"
            >
                <pre v-text="value" ></pre>
            </el-tab-pane>


        </el-tabs>
        <span slot="footer" class="dialog-footer">
    <el-button @click="yulanDialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="yulanDialogFormVisible = false">确 定</el-button>
  </span>
    </el-dialog>


    <div class="pagination-container">
        <el-pagination
                class="pagiantion"
                @current-change="handleCurrentChange"
                :current-page="pagination.currentPage"
                :page-size="pagination.pageSize"
                layout="total, prev, pager, next, jumper"
                :total="pagination.total">
        </el-pagination>
    </div>

</div>
</body>
<!-- import Vue before Element -->
<script src="hd/vue.js"></script>
<script src="hd/axios.js"></script>
<!-- import JavaScript -->
<script src="hd/element.js"></script>
<script>
    new Vue({
        el: '#app',
        data: function() {
            return {
                options:[],
                pagination: {//分页相关模型数据
                    page: 1,//当前页码
                    limit: 10,//每页显示的记录数
                    total: 100,//总记录数
                    queryString: null//查询条件
                },
                visible: false,
                tableData: [],
                multipleSelection: [],
                dialogFormVisible: false,
                form:{},
                title:"新增",
                yulanDialogFormVisible:false,
                activeName: 'Controller.java',
                codeView:{}
            }
        },
        created(){
            this.findPage();
            this.selectDict();
        },
        methods:{
            //生成 代码
            gen(){
                if (this.multipleSelection.length==0){
                    this.$message.error("请选择一条数据");
                }else {
                    axios({
                        method:'post',
                        url:"/generator/code",
                        data:this.multipleSelection,
                        responseType:'blob'
                    }).then(res=>{
                        let blob=new Blob([res.data],{type:res.data.type})
                        let filename = "code.zip";
                        //兼容ie
                        if(window.navigator && window.navigator.msSaveBlob){
                            window.navigator.msSaveBlob(blob,filename);
                        }else{
                            var downloadElement=document.createElement('a');//模拟一个a标签与asp.net试图操作类似
                            var href=window.URL.createObjectURL(blob);//转成链接让其能供人下载
                            downloadElement.href=href;//a标签的href
                            downloadElement.download=filename;//a标签的下载名字
                            document.body.appendChild(downloadElement);//注册这个控件将这个组件加到body尾部
                            downloadElement.click();//注销掉
                            window.URL.revokeObjectURL(href);//清除生成的链接，会占用一些东西，不知道啥，反正运行慢点
                        }
                    })
                }
            },
            //下拉列表
            selectDict(){
                axios.get("/ds/all").then(res=>{
                    this.options = res.data.list;
                })
            },
            submit(){
                axios.post("/config/submit",this.form).then(res=>{
                    this.$message.success("操作成功");
                    this.dialogFormVisible = false
                    this.findPage()
                })
            },

            handleSelectionChange:function (val){
                this.multipleSelection = val;
            },
            //分页查询
            findPage:function(){
                axios.get("/config/list",{
                    params:{
                        limit:this.pagination.limit,
                        page:this.pagination.page
                    }
                }).then(res=>{
                    this.tableData = res.data.page.list;
                    this.pagination.total = res.data.page.totalCount;
                })
            },
            //页码切换
            handleCurrentChange:function (val) {
                this.pagination.page = val;
                this.findPage();
            },
            add:function (){
                this.dialogFormVisible = true
                this.title = "新增"
                this.form = {}
            },

            remove:function(){
                if (this.multipleSelection.length==0){
                    this.$message.error("请选择一条数据");
                }else {
                    let ids = this.multipleSelection.map(item=>item.id);
                    this.del(ids)
                }
            },
            del:function (ids){
                axios.post("/config/delete",ids).then(res=>{
                    this.$message.success("操作成功");
                    this.findPage()
                })
            },
            handleEdit:function (row){
                this.dialogFormVisible = true
                this.title = "修改"
                this.form = row
            },
            handleDelete:function (row){
                console.log(row)
                let arr = [];
                arr.push(row.id)
                this.del(arr)
            },
            //在线预览
            yulan:function (row){
                axios.post("/generator/view",row).then(res=>{
                    console.log(res.data.data)
                    this.codeView = res.data.data
                })

                this.yulanDialogFormVisible = true
            },
            handleClick(tab, event) {
                console.log(tab, event);
            },
            /** 复制代码成功 */
            clipboardSuccess(){
                this.$message.success("复制成功");
            }
        }
    })
</script>
</html>